package cn.itsy.web.controller;

import cn.itsy.pojo.Duct;
import cn.itsy.pojo.Emp;
import cn.itsy.service.duct.DuctService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: MisterGay
 * @date: 2019-05-30 8:30
 */
@Controller
@RequestMapping(value = "duc")
public class DucController {
    @Resource(name = "ductServiceimpl")
    private DuctService ductService;

    /**
     * 准备修改，先查询职务类型F表
     *
     * @return
     */
    @RequestMapping(value = "queryDcuTypeAll", method = RequestMethod.GET)
    @ResponseBody
    public List<Duct> querydcuTypeAll() {
        String sql = "select duct.*,tname from duct,ductype where duct.ductype_id=ductype.id";
        List<Duct> ducts = this.ductService.queryAllDucType(sql);
        return ducts;
    }

    /**
     * 开始修改
     *
     * @param duct
     */
    @RequestMapping("updateDuctById")
    @ResponseBody
    public void updateDuctById(Duct duct, Integer oldId) {
        String sql = "update duct set duct.id=? ,duct.zbian=?, duct.zname=?,duct.ductype_id=?,duct.zdj=? where duct.id=?";
        boolean b = this.ductService.updateDuctNameById(sql, duct, oldId);
    }

    /**
     * 开始删除
     *
     * @param duct
     */
    @RequestMapping("deleteDuctypeById")
    @ResponseBody
    public String deleteDuctById(Duct duct) {
        /**
         * 先查需有没有员工在使用这个职务
         */
        String sql = "select * from emp where emp.duct_id=?";
        List<Emp> emps = this.ductService.queryEmpById(sql, duct);
        if (emps.isEmpty()) {
            String delSql = "delete from duct where duct.id=?";
            this.ductService.deleteDuctById(delSql, duct);
            return "success";
        } else {
            return "123";
        }
    }
}
